<template>
  <div>
    <div class="topBtn">
      <div class="sizeBig"><span>排程管理</span></div>
      <div>
        <el-button @click="getInitData" style="height:39px" type="primary">查询</el-button>
        <el-button @click="forcePrint" icon="el-icon-printer" style="height:39px" type="primary">强制打印</el-button>

        <el-button @click="goBackRePut" type="primary">
          <i class="icon-change"></i>退回重拼
        </el-button>

        <!--        <el-button @click="getLog" type="primary">-->
        <!--          <i class="icon-log"></i>排程操作日志-->
        <!--        </el-button>-->
        <!--        <el-button @click="changeLine" type="primary">-->
        <!--          <i class="icon-change"></i>换线-->
        <!--        </el-button>-->

        <el-button @click="Urgent" type="primary">
          <i class="icon-Urgent"></i>加急
        </el-button>
        <el-button @click="suspend" type="primary">
          <i class="icon-suspend"></i>暂停
        </el-button>
        <el-button @click="recovery" type="primary">
          <i class="icon-recovery"></i>恢复
        </el-button>
        <!-- <el-button type="primary" @click="switching">
            <i class="icon-switching"></i>排程切换</el-button> -->
      </div>
    </div>
    <div class="message">
      <p class="character">对生产排产进行监控和管理，确保产线生产正常运转。 <span class="skip">查看帮助文档</span></p>
    </div>
    <div class="inputcontent">
      <el-input @keydown.native.enter="init" class="inSearch" placeholder="请输入关键字检索" v-model="search"></el-input>
      <div @click="init" class="iconSearch"></div>
      <el-select @change="init" placeholder="状态" v-model="value1">
        <el-option
          :key="item.value"
          :label="item.label"
          :value="item.value"
          v-for="item in options1">
        </el-option>
      </el-select>
      <el-select @change="init" clearable placeholder="线体" v-model="value2">
        <el-option
          :key="index"
          :label="item.Name"
          :value="item.LineId"
          v-for="(item,index) in linebodyData">
        </el-option>
      </el-select>
    </div>
    <div class="table">
      <Table :reset="reset" id="out-table" v-on:changeData="changedata" v-on:getId="getListId"></Table>
    </div>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-size="size"
        :page-sizes="[10,20,50,100]"
        :total="TotalNum"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>

    <!-- 排程操作日志 -->
    <el-dialog :close-on-click-modal='false' :visible.sync="dialogLogVisible" center title="排程操作日志" width="700px">
      <Table1 :logData='logData'></Table1>
      <!-- 日志分页 -->
      <div class="block1">
        <el-pagination
          :current-page="currentPage1"
          :page-size="size1"
          :page-sizes="[10,20,50,100]"
          :total="TotalNum1"
          @current-change="handleCurrentChange1"
          @size-change="handleSizeChange1"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
      <!-- <div class="botBtn">
          <el-button class="quxiao" @click.native="dialogLogVisible=false">取消</el-button>
          <el-button class="queding" type="primary" @click="removeLogSubmit()">删除</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
  import {
    getProductionPlanDOList, //查询列表数据
    updateLineOfProductionPlan, //换线
    priorityOfProductionPlan, //加急
    pauseOfProductionPlan,  //暂停
    switchProductionPlan,  //排程切换
    getProductionPlanLog, //排程操作日志
    forcedToPrintOfProductionPlan, //强制打印
    recoverOfProductionPlan, ForcePrintProductPlan, DeleteProductPlan  // 恢复排程
  } from '@/api/schedule'
  import { getAllLineList, GetKMAnd750Line } from '@/api/linebody'
  import Table from './components/scheduleTable'
  import Table1 from './components/scheduleLogTable'

  import { delay } from '@/utils/autoSearch'
  import { parse } from 'path'
  import { constants } from 'crypto'
  import { ERR_OK } from '@/utils/commons'

  export default {
    name: 'schedule',
    components: {
      Table,
      Table1
    },
    data() {
      return {
        search: '',
        value1: '',
        value2: '',
        options1: [  //状态数据
          {
            value: -1,
            label: '所有'
          },
          {
            value: 0,
            label: '创建'
          },
          {
            value: 1,
            label: '合成中'
          },
          {
            value: 2,
            label: '已合成'
          },
          {
            value: 3,
            label: '打印中'
          },
          {
            value: 4,
            label: '打印暂停'
          },
          {
            value: 5,
            label: '打印取消'
          },
          {
            value: 6,
            label: '打印完成'
          },
          {
            value: 7,
            label: '暂停'
          }
        ],
        options2: [], // 线体数据
        checkIds: [], //勾选的id数组
        addlist: [], //勾选传过来的数据
        reset: [], //排程列表数据
        logData: [], //工位变更日志数据

        currentPage: 1, //初始页
        size: 10,
        TotalNum: 0,//数据总条数
        TotalNum1: 0,
        currentPage1: 1,
        size1: 10,
        dialogLogVisible: false, //排程操作日志弹出框
        linebodyData: []
      }
    },
    watch: {
      //用来监听页面变量的改变
      //监听搜索，300ms执行一次fetchData方法（去搜索）
      search() {
        delay(() => {
          this.fetchData()
        }, 300)
      }
    },
    mounted() {
      this.init()

      GetKMAnd750Line({ keyName: '', page: 1, pageSize: 99999999 }).then(res => {
        if (parseInt(res.Code) === ERR_OK) {
          this.linebodyData = res.Result
          // this.lineData = res.Result
          // console.log(this.lineData, 318)
        }
      })
    },
    // created(){
    //     this.onput();
    // },
    methods: {
      // 自动搜索的方法
      async fetchData(val) {
        this.init({ keyName: this.search })
      },
      // 分页
      handleSizeChange(size) {
        this.size = size
        getProductionPlanDOList({
          keyName: this.search,
          status: parseInt(this.value1),
          lineID: parseInt(this.value2),
          page: 1,
          pageSize: this.size
        }).then(res => {
          if (parseInt(res.Code) === 1) {
            this.reset = res.Result.Items
            this.TotalNum = res.Result.TotalNum
          }
        })
      },
      handleCurrentChange(currentPage) {
        this.currentPage = currentPage
        getProductionPlanDOList({
          keyName: this.search,
          status: parseInt(this.value1),
          lineID: parseInt(this.value2),
          page: this.currentPage,
          pageSize: this.size
        }).then(res => {
          if (parseInt(res.Code) === 1) {
            this.reset = res.Result.Items
            this.TotalNum = res.Result.TotalNum
          }
        })
      },
      //请求数据
      init() {
        // 排程列表数据
        getProductionPlanDOList({
          keyName: this.search,
          status: parseInt(this.value1),
          lineID: 0,
          page: 1,
          pageSize: this.size
        }).then(res => {
          if (parseInt(res.Code) === 1) {
            this.reset = res.Result.Items
            this.TotalNum = res.Result.TotalNum
          }
        })
        //线体列表数据
        getAllLineList({ keyName: '', lineType: 1 }).then(res => {
          if (parseInt(res.Code) === 1) {
            this.options2 = res.Result
          }
        })
      },
      //点击查询触发init事件
      getInitData() {
        getProductionPlanDOList({
          keyName: this.search,
          status: parseInt(this.value1),
          lineID: parseInt(this.value2),
          page: this.currentPage,
          pageSize: this.size
        }).then(res => {
          if (parseInt(res.Code) === 1) {
            this.reset = res.Result.Items
            this.TotalNum = res.Result.TotalNum
          }
        })
      },
      // onput(){
      //     //线体列表数据
      //     getLineList({keyName:'',page:1,pageSize:1000}).then((res)=>{
      //         if(parseInt(res.Code)===1){
      //             this.options2 = res.Result.Items;
      //         }
      //     })
      // },

      // 强制打印
      forcePrint() {
        if (this.addlist.length !== 1) {
          this.$message('请勾选一项')
        } else {
          ForcePrintProductPlan({ inProductPlanId: this.addlist[0].ProductionPlanId }).then(res => {
            if (parseInt(res.Code) === 1) {
              this.getInitData()
              this.$message({
                type: 'success',
                message: res.Message
              })
            }
          }).catch(e => {
            console.log(e)

          })
        }
      },
      getListId(listId) {
        this.checkIds = listId
      },
      changedata(data) {
        this.addlist = data
        console.log(this.addlist)
      },

      //排程操作日志
      getLog() {
        this.dialogLogVisible = true
        getProductionPlanLog({ businessType: 9, page: 1, pageSize: 10 }).then(res => {
          if (parseInt(res.Code) === 1) {
            this.logData = res.Result.Items
            this.TotalNum1 = res.Result.TotalNum
          }
        })
      },
      handleSizeChange1(size) {
        this.size1 = size
        getProductionPlanLog({ businessType: 9, page: 1, pageSize: this.size1 }).then(res => {
          if (parseInt(res.Code) === 1) {
            this.logData = res.Result.Items
            this.TotalNum1 = res.Result.TotalNum
          }
        })
      },
      handleCurrentChange1(currentPage) {
        this.currentPage1 = currentPage
        getProductionPlanLog({ businessType: 9, page: this.currentPage1, pageSize: this.size1 }).then(res => {
          if (parseInt(res.Code) === 1) {
            this.logData = res.Result.Items
            this.TotalNum1 = res.Result.TotalNum
          }
        })
      },

      //  重拼
      goBackRePut() {
        //  DeleteProductPlan ForcePrintProductPlan
        if (this.addlist.length !== 1) {
          this.$message('请勾选一项')
        } else {
          DeleteProductPlan({
            inProductPlanId: this.addlist[0].ProductionPlanId,
            inForceChange: false
          }).then(res => {
            if (parseInt(res.Code) === 1) {
              this.getInitData()
              this.$message({
                type: 'success',
                message: res.Message
              })
            }
          }).catch(e => {
            console.log(e)

          })
        }

      },

      //换线
      changeLine() {
        if (this.addlist.length !== 1) {
          this.$message('请勾选一项')
        } else if (this.value2 === '') {
          this.$message('请选择线体')
        } else {
          updateLineOfProductionPlan({
            productionPlanId: this.addlist[0].ProductionPlanId,
            lineId: this.value2
          }).then(res => {
            if (parseInt(res.Code) === 1) {
              this.$message({
                type: 'success',
                message: '换线成功'
              })
            }
            this.init()
          })
        }
      },
      //加急
      Urgent() {
        if (this.addlist.length !== 1) {
          this.$message('请勾选一项')
        } else {
          priorityOfProductionPlan({ productionPlanId: this.addlist[0].ProductionPlanId }).then(res => {
            if (parseInt(res.Code) === 1) {
              this.$message({
                type: 'success',
                message: '加急成功'
              })
            }
            this.init()
          })
        }

      },
      //暂停
      suspend() {
        if (this.addlist.length !== 1) {
          this.$message('请勾选一项')
        } else {
          if (this.addlist[0].Status === 8) {
            this.$message('已暂停')
          } else {
            pauseOfProductionPlan({ productionPlanId: this.addlist[0].ProductionPlanId }).then(res => {
              if (parseInt(res.Code) === 1) {
                this.$message({
                  type: 'success',
                  message: '暂停成功'
                })
              }
              this.init()
            })
          }
        }
      },
      //恢复排程
      recovery() {
        if (this.addlist.length !== 1) {
          this.$message('请勾选一项')
        } else {
          if (this.addlist[0].Status === 8) {
            this.$message('已恢复')
          } else {
            recoverOfProductionPlan({ productionPlanId: this.addlist[0].ProductionPlanId }).then(res => {
              if (parseInt(res.Code) === 1) {
                this.$message({
                  type: 'success',
                  message: '恢复排程成功'
                })
              }
              this.init()
            }).catch(res => {
            })
          }
        }
      }
      // 排程切换
      // switching(){
      //     if(this.addlist.length !== 1){
      //         this.$message('请勾选一项');
      //     }else{
      //         switchProductionPlan({productionPlanId:this.addlist[0].ProductionPlanId,nextProductionPlanNo:this.addlist[0].NextProductionPlanNo}).then(res => {
      //             if( parseInt(res.Code) === 1) {
      //                 this.$message({
      //                     type:'success',
      //                     message:'排程切换成功'
      //                 })
      //             }
      //             this.init()
      //         })
      //     }
      // }
    }
  }
</script>

<style lang="scss" scoped>
  .sizeBig {
    font-size: 16px;
  }

  .topBtn {
    height: 60px;
    width: 98%;
    margin: 0 auto;
    line-height: 60px;
    display: flex;
    justify-content: space-between;

    .icon-log {
      display: inline-block;
      width: 14px;
      height: 14px;
      margin: 0 3px 2px 0;
      background: url("image/排程操作日志.png") no-repeat;
      vertical-align: middle;
    }

    .icon-change {
      display: inline-block;
      width: 14px;
      height: 14px;
      margin: 0 3px 2px 0;
      background: url("image/huanxian.png") no-repeat;
      vertical-align: middle;
    }

    .icon-Urgent {
      display: inline-block;
      width: 14px;
      height: 14px;
      margin: 0 3px 2px 0;
      background: url("image/jiaji.png") no-repeat;
      vertical-align: middle;
    }

    .icon-suspend {
      display: inline-block;
      width: 14px;
      height: 14px;
      margin: 0 3px 2px 0;
      background: url("image/zanting.png") no-repeat;
      vertical-align: middle;
    }

    .icon-recovery {
      display: inline-block;
      width: 14px;
      height: 14px;
      margin: 0 3px 2px 0;
      background: url("image/renwukaishi.png") no-repeat;
      vertical-align: middle;
    }

    .icon-switching {
      display: inline-block;
      width: 14px;
      height: 14px;
      margin: 0 3px 2px 0;
      background: url("image/转生产计划.png") no-repeat;
      vertical-align: middle;
    }
  }

  .message {
    width: 98%;
    margin: 0 auto;
    height: 60px;
    background: rgba(255, 255, 255, 1);
    border: 1px solid rgba(238, 238, 238, 1);
    font-size: 14px;

    .character {
      margin: 20px 0 21px 20px;
      color: #999999;

      .skip {
        color: #0095FF;
        margin-left: 10px;
        cursor: pointer;
      }
    }
  }

  .inputcontent {
    width: 98%;
    margin: 0 auto;
    height: 80px;
    position: relative;
    padding-top: 21px;
    box-sizing: border-box;

    .iconSearch {
      position: absolute;
      left: 277px;
      top: 30px;
      width: 20px;
      height: 20px;
      background-image: url('./image/搜索.png');
      background-repeat: no-repeat;
      cursor: pointer;
    }

    .inSearch {
      width: 308px;
      height: 40px;
    }
  }

  .table {
    width: 98%;
    margin: 0 auto;
  }

  .block {
    text-align: right;
    margin: 28px 20px 0 0;
  }

  .block1 {
    margin-top: 10px;
    margin-left: 110px;
    // float: right;
  }

  .botBtn {
    text-align: center;
    //   height: 60px;
    margin-top: 10px;

    .quxiao {
      margin-right: 20px;
    }

    .queding {
      margin-left: 20px;
    }
  }
</style>
